<?
    /* Crash report helper */
    function parse_crash_report($file) {
        $handle = @fopen($file, "r");
        if (empty($handle)) {
            return NULL;
        }
        $crash_reports = array();
        while (!feof($handle)) {
            $buffer = fgets($handle, 4096);
            // New crash report
            if (strstr($buffer, "Time:")) {
                $crash_time = trim(substr($buffer, strpos($buffer, ' ') + 1));
                $crash = _parse_one($handle, $buffer);
                if ($crash != NULL) {
                    $crash->crash_time = $crash_time;
                    $crash_reports[] = $crash;
                }
            }
        }
        fclose($handle);
        return $crash_reports;
    }
    function _parse_one(&$handle, $pre_buffer) {
        if (empty($handle) || feof($handle)) {
            return NULL;
        }
        $file_content = "";
        $file_content .= $pre_buffer;
        // Read the header of crash report file
        $header_buffer= _read_header($handle);
        // Check if this crash is java crash
        if (_is_java_crash($header_buffer)) {
            return _parse_java($handle, $header_buffer, $file_content);
        } else {
            return _parse_native($handle, $header_buffer, $file_content);
        }
    }
    function _read_header(&$handle) {
        if (empty($handle) || feof($handle)) {
            return NULL;
        }
        $header_content = "";
        $stop = FALSE;
        while (!$stop && !feof($handle)) {
            $buffer = fgets($handle, 4096);
            $header_content .= $buffer;
            if ($buffer == "\n")
                $stop = TRUE;
        }
        return $header_content;
    }
    function _is_java_crash($buffer) {
        if (strstr($buffer, "Package:") != FALSE) {
            return TRUE;
        }
        return FALSE;
    }
    function _parse_native(&$handle, $header_buffer, &$file_content) {
        // Get first name for parsing meta data
        $stop = FALSE;
        $header_array = split("\n", $header_buffer);
        $buffer = $header_array[0];
        $file_content .= $header_buffer;
        $crash_report = new crash_report();
        if (preg_match( "/Build description:\s(.*?)(\s)/", $buffer, $matches )) {
            $crash_report->variant = trim($matches[1]);
        }
        if (preg_match( "/\d{1,}\.\d{2}/", $buffer, $matches )) {
            $crash_report->sw_version = trim($matches[0]);
        }
        $build_time = "";
        $crash_report->process_type = PROCESS_TYPE_NATIVE;
        if (preg_match( "/\d{14}(\s)/", $buffer, $matches )) {
            $build_sn = $matches[0];
            $year = substr($build_sn, 0, 4);
            $month = substr($build_sn, 4, 2);
            $day = substr($build_sn, 6, 2);
            $crash_report->build_time = trim($year."-".$month."-".$day);
        }
        $count = 0;
        while (!$stop && !feof($handle)) {
            $buffer = fgets($handle, 4096);
            // If parse end
            // JB is 3, but KK is 2
            if ($count == 3 || $count == 2) {
                if (preg_match( "/>>>(.*?)<<</", $buffer, $matches )) {
                    $crash_report->process = trim($matches[1]);
                }
                if (preg_match( "/^pid:\s(.*?),/", $buffer, $matches )) {
                    $crash_report->pid = trim($matches[1]);
                }
            }
            if (strstr($buffer, "=====") != FALSE) {
                $stop = TRUE;
            } else {
                $file_content .= $buffer;
            }
            $count++;
        }
        $crash_report->crash_report = $file_content;
        return $crash_report;
    }

    function _parse_java(&$handle, $header_buffer, &$file_content) {
        // Get first name for parsing meta data
        $crash_report = new crash_report();
        $stop = FALSE;
        $header_array = split("\n", $header_buffer);
        $buffer = $header_array[0];
        $file_content .= $header_buffer;
        if (preg_match( "/Build description:\s(.*?)(\s)/", $buffer, $matches )) {
            $crash_report->variant = trim($matches[1]);
        }
        if (preg_match( "/\d{1,}\.\d{2}/", $buffer, $matches )) {
            $crash_report->sw_version = trim($matches[0]);
        }
        $build_time = "";
        $crash_report->process_type = PROCESS_TYPE_JAVA;
        if (preg_match( "/\d{14}(\s)/", $buffer, $matches )) {
            $build_sn = $matches[0];
            $year = substr($build_sn, 0, 4);
            $month = substr($build_sn, 4, 2);
            $day = substr($build_sn, 6, 2);
            $crash_report->build_time = trim($year."-".$month."-".$day);
        }
        foreach ($header_array as $buffer) {
            if (preg_match( "/^Process: (.*?)$/", $buffer, $matches )) {
                $crash_report->process = trim($matches[1]);
            }
        }
        $crash_report->pid = 0;
        $count = 0;
        while (!$stop && !feof($handle)) {
            $buffer = fgets($handle, 4096);
            if (strstr($buffer, "=====") != FALSE) {
                $stop = TRUE;
            } else {
                $file_content .= $buffer;
            }
            $count++;
        }
        $crash_report->crash_report = $file_content;
        return $crash_report;
    }
?>